package com.trkj.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.trkj.pojo.Contract;
import com.trkj.service.ContractService;
import com.trkj.vo.ContractEmpVo;

@Controller
@SessionAttributes({"funcIds"})
public class ContractContruller {
	/**
	 * @author csj
	 */
	@Autowired
	ContractService contractService;
	
	/**
	 * 调用合同管理主界面
	 * @author csj
	 * @return
	 */
	@GetMapping("ContractManagement")
	public String ContractManagement() {
		return "ContractManagement";
	}
	
	/**
	 * 多表分页带条件模糊查询用户信息
	 * @return 
	 * @return 
	 * 
	 * @return
	 */
	@RequestMapping("/ContractInfo")
	public @ResponseBody Map<String, Object> ContractInfo(String empName, String contractType, String contractState, Integer page,
			Integer rows,@RequestParam(required=false,defaultValue="contractId") String sort,
			@RequestParam(required=false,defaultValue="desc") String order) {
			return contractService.pageSelectContract(empName, contractType, contractState, page, rows, sort, order);
	}
	
	/**
	 * 调用未签合同人员的信息界面
	 * @author csj
	 * @return
	 */
	@GetMapping("Unsigned")
	public String Unsigned() {
		return "ContractUnsigned";
	}
	
	/**
	 * 查询未签人员的姓名与编号
	 * @author csj
	 * @param deptName
	 * @param empName
	 * @return
	 */
	@RequestMapping("/UnsignedInfo")
	public @ResponseBody List<Map<String, Object>>  UnsignedInfo(String deptName,String empName) {
		return contractService.findUnsignedInfo(deptName,empName);
	}
	
	/**
	 * 
	 * @author csj
	 * @param empName
	 */
	/*@RequestMapping("/addUnsignedContract")
	public @ResponseBody void  addUnsignedContract(String empName) {
		
	}
	*/
	/**
	 * 调用添加未签人员的表单界面
	 * @return
	 */
	@GetMapping("addUnsignedContract")
	public String addUnsignedContract() {
		return "addUnsignedContract";
	}
	
	/**
	 * 传输添加未签人员的表单界面的所需的信息
	 * @author csj
	 * @param empId
	 * @return
	 */
	
	@RequestMapping("/chaxunContractInfo")
	@ResponseBody
	public ContractEmpVo chaxunContractInfo(@RequestParam("empId") Integer empId) {
		return contractService.checkempInfo(empId);
	}
	
	
	/**
	 * 传输从添加未签人员的表单中要保存到的信息
	 * @author csj
	 * @param contract
	 * @param empId
	 * @return 
	 * @return
	 */
	@RequestMapping("/saveUnsignedInfo")
	@ResponseBody
	public boolean saveUnsignedInfo(Contract contract,@RequestParam("empId") Integer empId) {
		System.out.println(empId);
		System.out.println(contract.getContractDeadtime());
		System.out.println(contract.getContractSignedtime());
		Integer issave = contractService.saveUnsigned(contract,empId);
		if(issave!=0) {
			return true;
		}
		return false;
	}
	
	/**
	 * 调用续签合同人员的信息界面
	 * @author csj
	 * @return
	 */
	@GetMapping("/RenewContract")
	public String RenewContract() {
		return "RenewContract";
	}
	
	@RequestMapping("/RenewContractInfo")
	public @ResponseBody List<Map<String, Object>> RenewContractInfo(String deptName,String empName) {
		 List<Map<String, Object>> RenewInfo = contractService.checkrenewContractlist(empName, deptName);
		return RenewInfo;
	}
	
	/**
	 * 调用续签人员增加界面
	 * @author csj
	 * @return
	 */
	@GetMapping("/addRenewContract")
	public String addRenewContract() {
		return "addRenewContract";
	}
	
	@RequestMapping("/checkempAndcontactInfo")
	@ResponseBody
	public ContractEmpVo checkempAndcontactInfo(@RequestParam("contractId") Integer contractId) {
		return contractService.checkempAndcontactInfo(contractId);
	}
	
	@GetMapping("/editContract")
	public String editContract() {
		return "editContract";
	}
	
	@RequestMapping("/editContractInfo")
	@ResponseBody
	public ContractEmpVo editContractInfo(@RequestParam("contractId") Integer contractId) {
		return contractService.checkcontactInfo(contractId);
	}
	
	@PostMapping("/RelieveContract")
	public void RelieveContract(@RequestParam("contractId") Integer contractId,HttpServletResponse response) {
		boolean  relieve= contractService.RelieveContract(contractId);
		try {
			response.getWriter().write(relieve+"");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
